/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    gpio.c
  * @brief   This file provides code for the configuration
  *          of all used GPIO pins.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "gpio.h"

/* USER CODE BEGIN 0 */

#include "cmsis_os.h"
#include <string.h>


/* USER CODE END 0 */

/*----------------------------------------------------------------------------*/
/* Configure GPIO                                                             */
/*----------------------------------------------------------------------------*/
/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/** Configure pins as
        * Analog
        * Input
        * Output
        * EVENT_OUT
        * EXTI
*/
void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, GREEN_LED_Pin|RED_LED_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin : PtPin */
  GPIO_InitStruct.Pin = BLUE_LED_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(BLUE_LED_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : PBPin PBPin PBPin */
  GPIO_InitStruct.Pin = key1_Pin|key2_Pin|key3_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /*Configure GPIO pins : PCPin PCPin */
  GPIO_InitStruct.Pin = GREEN_LED_Pin|RED_LED_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI15_10_IRQn, 5, 0);
  HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);

}

/* USER CODE BEGIN 2 */



#if 0
/*led*/
gpio_t	leds[LedMax] =
{
	{"BLUE_LED", BLUE_LED_GPIO_Port,  BLUE_LED_Pin},
	{"GREEN_LED", GREEN_LED_GPIO_Port,  GREEN_LED_Pin},
	{"RED_LED", RED_LED_GPIO_Port,  RED_LED_Pin},
};

gpio_t	relays[RelayMax] =
{
		{"Relay1", Relay1_GPIO_Port, Relay1_Pin},

};
#endif


typedef struct gpio_s
{
	GPIO_TypeDef   *group;
	uint16_t       pin;
} gpio_t;

gpio_t	leds[LedMax] =
{
	{BLUE_LED_GPIO_Port,  BLUE_LED_Pin},
	{GREEN_LED_GPIO_Port,  GREEN_LED_Pin},
	{RED_LED_GPIO_Port,  RED_LED_Pin},
};



void turn_led(int which, int status)
{
	GPIO_PinState       level;
	if( which<0 || which>=LedMax)
	{
		return ;
	}
	level = status == OFF ? GPIO_PIN_SET : GPIO_PIN_RESET;

	HAL_GPIO_WritePin(leds[which].group, leds[which].pin, level);
}


void blink_led(int which, uint32_t interval)
{
	if(which>=LedMax || interval<=0)
	{
		return ;
	}

	turn_led(which, ON);
	//HAL_Delay(interval);
	osDelay(interval);

	turn_led(which, OFF);
	//HAL_Delay(interval);
	osDelay(interval);
}





#if 0
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	if (key1_Pin == GPIO_Pin)
	{

		if (GPIO_PIN_RESET == HAL_GPIO_ReadPin(key1_GPIO_Port, key1_Pin))
		{

			if (GPIO_PIN_RESET == HAL_GPIO_ReadPin(key1_GPIO_Port, key1_Pin))
			{
				keyStatus = key_down;
			}
		}
		else
		{

			if (GPIO_PIN_SET == HAL_GPIO_ReadPin(key1_GPIO_Port, key1_Pin))
			{

				if (GPIO_PIN_SET == HAL_GPIO_ReadPin(key1_GPIO_Port, key1_Pin))
				{
					keyStatus = key_up;
				}
			}
		}
	}
}


	else if (key2_Pin == GPIO_Pin)
	{
		state ^= 1;

		if ( state )
		{
			HAL_GPIO_WritePin(GPIOC, GREEN_LED_Pin, GPIO_PIN_RESET);
		}
		else
		{
			HAL_GPIO_WritePin(GPIOC, GREEN_LED_Pin, GPIO_PIN_SET);
		}
	}
	else if (key3_Pin == GPIO_Pin)
	{
		state ^= 1;

		if ( state )
		{
			HAL_GPIO_WritePin(GPIOC, RED_LED_Pin, GPIO_PIN_RESET);
		}
		else
		{
			HAL_GPIO_WritePin(GPIOC, RED_LED_Pin, GPIO_PIN_SET);
		}
	}

}

#endif

#if 0
void turn_relay(int which, int status)
{
	GPIO_PinState       level;
	if( which<0 || which>=RelayMax)
	{
		return ;
	}
	level = status == OFF ? GPIO_PIN_SET : GPIO_PIN_RESET;

	HAL_GPIO_WritePin(relays[which].group, relays[which].pin, level);
}
#endif








#if 0
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	static uint8_t state = 0;

	if (key1_Pin == GPIO_Pin)
	{
		state ^= 1;

		if ( state )
		{
			 HAL_GPIO_WritePin(GPIOB, BLUE_LED_Pin, GPIO_PIN_RESET);
		}
		else
		{
			HAL_GPIO_WritePin(GPIOB, BLUE_LED_Pin, GPIO_PIN_SET);
		}
	}
	else if (key2_Pin == GPIO_Pin)
	{
		state ^= 1;

		if ( state )
		{
			HAL_GPIO_WritePin(GPIOC, GREEN_LED_Pin, GPIO_PIN_RESET);
		}
		else
		{
			HAL_GPIO_WritePin(GPIOC, GREEN_LED_Pin, GPIO_PIN_SET);
		}
	}
	else if (key3_Pin == GPIO_Pin)
	{
		state ^= 1;

		if ( state )
		{
			HAL_GPIO_WritePin(GPIOC, RED_LED_Pin, GPIO_PIN_RESET);
		}
		else
		{
			HAL_GPIO_WritePin(GPIOC, RED_LED_Pin, GPIO_PIN_SET);
		}
	}

}

#endif
/* USER CODE END 2 */
